package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.model.admin.AdminStatusParam;
import com.yungu.swift.system.sys.model.dto.SysRegionalAgentDto;
import com.yungu.swift.system.sys.model.param.AdminAgentPageParam;
import com.yungu.swift.system.sys.model.param.AdminAgentParam;
import com.yungu.swift.system.sys.model.vo.RegionalAgentDetailVo;
import com.yungu.swift.system.sys.model.vo.RegionalAgentVo;
import com.yungu.swift.system.sys.service.SysRegionalAgentService;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : cuixiuyin
 * @date : 2019/6/20
 */
@RestController
@RequestMapping("/admin/v1/operation/agent")
@Api(value = "【运营管理】", tags = "区域代理管理")
public class RegionalAgentAdmin {

    @Reference
    private SysRegionalAgentService sysRegionalAgentService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "区域代理分页查询", httpMethod = "POST")
    public ResponseData<PageVo<RegionalAgentVo>> queryPage(@RequestBody AdminAgentPageParam agentPageParam) {
        return sysRegionalAgentService.queryPageVo(agentPageParam);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "区域代理信息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminAgentParam agentParam) {
        SysRegionalAgentDto regionalAgentDto = new SysRegionalAgentDto();
        BeanUtils.copyProperties(agentParam, regionalAgentDto);
        if (StringUtils.isEmpty(regionalAgentDto.getUuid())) {
            regionalAgentDto.setUuid(StringUtils.buildUUID());
            regionalAgentDto.setCreateOn(new Date());
            regionalAgentDto.setCreateBy(agentParam.getUserUuid());
            regionalAgentDto.setAppid(agentParam.getAppid());
            regionalAgentDto.setStatus(0);
            return sysRegionalAgentService.add(regionalAgentDto);
        }
        regionalAgentDto.setCreateOn(new Date());
        regionalAgentDto.setUpdateBy(agentParam.getUserUuid());
        return sysRegionalAgentService.edit(regionalAgentDto);
    }

    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
    @ApiOperation(value = "区域代理信息开通或关闭", httpMethod = "POST")
    public ResponseData<Boolean> changeStatus(@RequestBody AdminStatusParam statusParam) {
        if (ParamUtil.isIllegal(statusParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysRegionalAgentService.changeStatus(statusParam.getUuid(), statusParam.getStatus(), statusParam.getUserUuid());
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "区域代理信息详情", httpMethod = "GET")
    public ResponseData<RegionalAgentDetailVo> detail(@RequestParam String uuid, @CurrentUser AdminParam userParam) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysRegionalAgentService.detail(uuid, userParam.getAppid());
    }
}
